查看原文
其他

设计模式-命令模式

JavaStorm 码哥字节 2022-10-28

 关注公众号 JavaStorm 获取更多成长。

命令模式把一个请求或者操作封装到一个对象中。命令模式允许系统使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。

GitHub地址: https://github.com/UniqueDong/zero-design-stu 中的 headfirst 包下代码。

概述

命令模式是对命令的封装。命令模式把发出命令的责任和执行命令的责任分割开,委派给不同的对象。

每一个命令都是一个操作:请求的一方发出请求要求执行一个操作;接收的一方收到请求,并执行操作。命令模式允许请求的一方和接收的一方独立开来,使得请求的一方不必知道接收请求的一方的接口,更不必知道请求是怎么被接收,以及操作是否被执行、何时被执行,以及是怎么被执行的。

  命令允许请求的一方和接收请求的一方能够独立演化,从而具有以下的优点:

  (1)命令模式使新的命令很容易地被加入到系统里。

  (2)允许接收请求的一方决定是否要否决请求。

  (3)能较容易地设计一个命令队列。

  (4)可以容易地实现对请求的撤销和恢复。

  (5)在需要的情况下,可以较容易地将命令记入日志。

角色

  • 客户端(Client)角色: 创建一个 ConcreteCommand,并设置其接受者。

  • 命令(Command)角色: 为所有的命令申明一个接口。调用命令对象的 execute 方法就可以让接受者执行相关的动作,同事接口还具备一个 undo() 撤回方法。

  • 具体命令(ConcreteCommand)角色: 定义一个接收者和行为之间的弱耦合;实现execute()方法,负责调用接收者的相应操作。execute()方法通常叫做执行方法。调用者只需要调用 execute 方法就可以发出请求,然后由 ConcreteCommand 调用接受者的一个或者多个动作。

  • 调用者(Invoker)角色: 调用者持有一个命令对象,提供一个触发方法调用命令对象的 execute 方法,将命令执行。

  • 接收者(Receiver)角色: 负责具体实施和执行一个请求。任何一个类都可以成为接收者,实施和执行请求的方法叫做行动方法。

执行流程

  1. 客户端创建一个命令对象。

  2. 客户端在调用者对象上调用 setCommand 方法。

  3. 在未来合适的时间点,调用者调用命令对象的 execute 方法。

  4. 命令通过调用者委托到对应的接受者执行。完成任务。

场景模拟

一个全能遥控器 6个可编程插槽(每个可以指定一个不同的家电装置),用来控制家电(电视、空调、冰箱、音响)。每个插槽有对应的 [开] 和 [关] 按钮。同时还具备一个整体一键撤回按钮。撤回需求是这样的,比如电灯是关的,然后按下开启按钮电灯就开了。现在假如按下撤销按钮,那么上一个动作将会翻转。在这里,电灯将会关闭。


插槽连接对应的家电,开关是对应的指令。每个家电对应两个指令,分别是 【开】和【关】按键。

许多家电都有 on() 和 off() 方法,除此之外还有一些 setVolumn()、setTV()、setTemperature() 方法。 我们总不能 写 if slot1 == Light then light.on()。

代码实现

命令接受者角色

首先我们拥有很多家电。他们其实就是不同命令的接受者执行。

  1. package com.zero.headfirst.command.receiver;


  2. public class Light {

  3. public void on() {

  4. System.out.println("打开电灯。");

  5. }

  6. public void off() {

  7. System.out.println("关灯。");

  8. }

  9. }

  • 音响

  1. package com.zero.headfirst.command.receiver;


  2. public class Stereo {

  3. public void on() {

  4. System.out.println("打开音响");

  5. }


  6. public void off() {

  7. System.out.println("关闭音响");

  8. }


  9. public void setCD() {

  10. System.out.println("放入CD");

  11. }


  12. public void setVolume() {

  13. System.out.println("音响音量设置为20");

  14. }

  15. }

命令角色

首先让所有的命令对象实现该接口,分别有命令执行与撤回

  1. package com.zero.headfirst.command;


  2. /**

  3. * 命令(Command)角色

  4. */

  5. public interface Command {

  6. /**

  7. * 命令执行

  8. */

  9. void execute();


  10. /**

  11. * 命令撤销

  12. */

  13. void undo();

  14. }

具体命令角色

  • 定义开灯命令,实现 execute 。持有 命令接受者 灯的引用,从而当调用者调用 execute 将委托给对应的 灯执行开灯操作。

  1. package com.zero.headfirst.command.impl;


  2. import com.zero.headfirst.command.Command;

  3. import com.zero.headfirst.command.receiver.Light;


  4. public class LightOnCommand implements Command {


  5. /**

  6. * 持有接受者实例,以便当命令execute执行的时候由接受者执行开灯

  7. */

  8. private Light light;


  9. @Override

  10. public void execute() {

  11. light.on();

  12. }


  13. @Override

  14. public void undo() {

  15. light.off();

  16. }


  17. /**

  18. * 设置命令的接受者

  19. * @param light

  20. */

  21. public void setLight(Light light) {

  22. this.light = light;

  23. }

  24. }

  • 定义关灯命令

  1. package com.zero.headfirst.command.impl;


  2. import com.zero.headfirst.command.Command;

  3. import com.zero.headfirst.command.receiver.Light;


  4. public class LightOffCommand implements Command {


  5. /**

  6. * 持有接受者实例,以便当命令execute执行的时候由接受者执行

  7. */

  8. private Light light;


  9. @Override

  10. public void execute() {

  11. light.off();

  12. }


  13. @Override

  14. public void undo() {

  15. light.on();

  16. }


  17. public void setLight(Light light) {

  18. this.light = light;

  19. }

  20. }

  • 定义打开音响命令

  1. package com.zero.headfirst.command.impl;


  2. import com.zero.headfirst.command.Command;

  3. import com.zero.headfirst.command.receiver.Stereo;


  4. /**

  5. * 音响开指令

  6. */

  7. public class StereoOnCommand implements Command {


  8. private Stereo stereo;


  9. @Override

  10. public void execute() {

  11. stereo.on();

  12. stereo.setCD();

  13. stereo.setVolume();

  14. }


  15. @Override

  16. public void undo() {

  17. stereo.off();

  18. }


  19. public void setStereo(Stereo stereo) {

  20. this.stereo = stereo;

  21. }

  22. }

  • 定义关闭音响命令

  1. package com.zero.headfirst.command.impl;


  2. import com.zero.headfirst.command.Command;

  3. import com.zero.headfirst.command.receiver.Stereo;


  4. public class StereoOffCommand implements Command {


  5. private Stereo stereo;


  6. public void setStereo(Stereo stereo) {

  7. this.stereo = stereo;

  8. }


  9. @Override

  10. public void execute() {

  11. stereo.off();

  12. }


  13. @Override

  14. public void undo() {

  15. stereo.on();

  16. stereo.setCD();

  17. stereo.setVolume();

  18. }

  19. }

剩下的打开电视机、关闭电视机、打开空调、关闭空调的就不一一写了。都是一样的模板套路。具体代码可以查阅 GitHub地址: https://github.com/UniqueDong/zero-design-stu 中的 headfirst 包下代码。

调用者角色

其实就是我们的遥控器。

  1. package com.zero.headfirst.command;


  2. import com.zero.headfirst.command.impl.NoCommand;


  3. import java.util.Arrays;


  4. /**

  5. * 调用者:遥控器

  6. */

  7. public class RemoteControl {

  8. /**

  9. * 一共4个家电插槽,每个插槽有 开与关命令。

  10. */

  11. private Command[] onCommands;

  12. private Command[] offCommands;


  13. //用来保存前一个命令,用来实现撤销功能

  14. private Command undoCommand;


  15. /**

  16. * 通过构造器初始化开关数组

  17. */

  18. public RemoteControl() {

  19. onCommands = new Command[4];

  20. offCommands = new Command[4];

  21. //初始化所有插槽为空指令

  22. Command noCommand = new NoCommand();

  23. for (int i = 0; i < 4; i++) {

  24. onCommands[i] = noCommand;

  25. offCommands[i] = noCommand;

  26. }

  27. //一开始没有所谓的前一个命令,所以默认无指令

  28. undoCommand = noCommand;

  29. }


  30. /**

  31. * 设置指定插槽对应的按钮指令

  32. * @param slot 插槽位置

  33. * @param onCommand 开指令

  34. * @param offCaommand 关指令

  35. */

  36. public void setCommand(int slot,Command onCommand, Command offCaommand) {

  37. onCommands[slot] = onCommand;

  38. offCommands[slot] = offCaommand;

  39. }


  40. /**

  41. * 模拟按下指定插槽对应的【开】按键

  42. */

  43. public void pressOnButton(int slot) {

  44. onCommands[slot].execute();

  45. //将当前指令记录下来,用于在撤销的时候能执行命令对应的 undo 方法从而实现撤销功能

  46. undoCommand = onCommands[slot];

  47. }


  48. /**

  49. * 模拟按下指定插槽对应的【关】按键

  50. */

  51. public void pressOffButton(int slot) {

  52. offCommands[slot].execute();

  53. undoCommand = offCommands[slot];

  54. }


  55. /**

  56. * 撤销功能

  57. */

  58. public void pressUndoButton() {

  59. undoCommand.undo();

  60. }


  61. @Override

  62. public String toString() {

  63. return "RemoteControl{" +

  64. "onCommands=" + Arrays.toString(onCommands) +

  65. ", offCommands=" + Arrays.toString(offCommands) +

  66. '}';

  67. }

  68. }

客户端角色

获取遥控器,并且拿到灯、空调等命令接受者。分别创建对应的 【开】,【关】指令。 链接到对应的插槽。当按下按钮的时候触发指定的指令。

  1. package com.zero.headfirst.command;


  2. import com.zero.headfirst.command.impl.*;

  3. import com.zero.headfirst.command.receiver.AirConditioning;

  4. import com.zero.headfirst.command.receiver.Light;

  5. import com.zero.headfirst.command.receiver.Stereo;

  6. import com.zero.headfirst.command.receiver.TV;


  7. /**

  8. * 客户端角色

  9. */

  10. public class CommandClient {

  11. public static void main(String[] args) {

  12. //创建一个遥控器-调用者角色

  13. RemoteControl remoteControl = new RemoteControl();

  14. //1. 创建电灯-接受者角色

  15. Light light = new Light();

  16. //创建开灯、关灯命令-命令具体角色

  17. LightOnCommand lightOnCommand = new LightOnCommand();

  18. lightOnCommand.setLight(light);

  19. LightOffCommand lightOffCommand = new LightOffCommand();

  20. lightOffCommand.setLight(light);


  21. //调用者设置电灯插槽以及对应的开关按键指令-调用者角色

  22. remoteControl.setCommand(0, lightOnCommand, lightOffCommand);


  23. // 2. 设置音响插槽与对应按键指令

  24. Stereo stereo = new Stereo();

  25. StereoOnCommand stereoOnCommand = new StereoOnCommand();

  26. stereoOnCommand.setStereo(stereo);

  27. StereoOffCommand stereoOffCommand = new StereoOffCommand();

  28. stereoOffCommand.setStereo(stereo);


  29. remoteControl.setCommand(1, stereoOnCommand, stereoOffCommand);


  30. //3. 空调

  31. AirConditioning airConditioning = new AirConditioning();

  32. AirConditioningOnCommand airConditioningOnCommand = new AirConditioningOnCommand();

  33. airConditioningOnCommand.setAirConditioning(airConditioning);

  34. AirConditioningOffCommand airConditioningOffCommand = new AirConditioningOffCommand();

  35. airConditioningOffCommand.setAirConditioning(airConditioning);


  36. remoteControl.setCommand(2, airConditioningOnCommand, airConditioningOffCommand);


  37. //4. 电视

  38. TV tv = new TV();

  39. TVOnCommand tvOnCommand = new TVOnCommand();

  40. tvOnCommand.setTv(tv);

  41. TVOffCommand tvOffCommand = new TVOffCommand();

  42. tvOffCommand.setTv(tv);


  43. remoteControl.setCommand(3, tvOnCommand, tvOffCommand);


  44. //模拟按键

  45. System.out.println("-------码农回家了,使用遥控开启电灯、音响、空调、电视----");

  46. remoteControl.pressOnButton(0);

  47. remoteControl.pressOnButton(1);

  48. remoteControl.pressOnButton(2);

  49. remoteControl.pressOnButton(3);


  50. System.out.println("------码农睡觉了,使用遥控关闭电灯、音响、电视。不关空调--------");

  51. remoteControl.pressOffButton(0);

  52. remoteControl.pressOffButton(1);

  53. remoteControl.pressOffButton(3);


  54. System.out.println("----撤销测试,先打开电灯。再关闭电灯。然后按撤销----");

  55. remoteControl.pressOnButton(0);

  56. remoteControl.pressOffButton(0);

  57. //一键撤销

  58. remoteControl.pressUndoButton();

  59. }

  60. }

测试结果

  1. -------码农回家了,使用遥控开启电灯、音响、空调、电视----

  2. 打开电灯。

  3. 打开音响

  4. 放入CD

  5. 音响音量设置为20

  6. 打开空调

  7. 空调温度设置28°

  8. 打开电视

  9. 设置频道为宇宙电视台

  10. 电视音量设置为20

  11. ------码农睡觉了,使用遥控关闭电灯、音响、电视。不关空调--------

  12. 关灯。

  13. 关闭音响

  14. 关闭电视

  15. ----撤销测试,先打开电灯。再关闭电灯。然后按撤销----

  16. 打开电灯。

  17. 关灯。

  18. 打开电灯。

总结

使用场景:

  1. 工作队列:在某一端添加指令,只要是实现命令模式的对象都可以放到队列里。另外一端是线程。线程进项下面的工作:从队列取出一个命令,然后调用execute 方法,调用完后将该命令丢弃,再继续取下一个命令。

  2. 线程池。

关注公众号 JavaStorm 获取更多模式


您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存